package com.drinkdelivery.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
//import org.springframework.security.access.prepost.PreAuthorize;
import com.drinkdelivery.common.request.ProductCategoriesRequest;
import com.drinkdelivery.service.IProductCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.drinkdelivery.common.annotation.Log;
import com.drinkdelivery.common.core.controller.BaseController;
import com.drinkdelivery.common.core.domain.AjaxResult;
import com.drinkdelivery.common.enums.BusinessType;
import com.drinkdelivery.common.domain.ProductCategories;

import com.drinkdelivery.common.utils.poi.ExcelUtil;
import com.drinkdelivery.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 *
 * @author lzh
 * @date 2024-11-04
 */
@Api(tags = "商品分类Controller")
@RestController
@RequestMapping("/admin/categories")
public class ProductCategoriesController extends BaseController
{
    @Autowired
    private IProductCategoriesService productCategoriesService;

    /**
     * 查询商品分类列表
     */
    @ApiOperation(value = "查询商品分类列表")
//@PreAuthorize("@ss.hasPermi('drinkdelivery:categories:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductCategories productCategories)
    {
        startPage();
        List<ProductCategories> list = productCategoriesService.selectProductCategoriesList(productCategories);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @ApiOperation(value = "导出商品分类列表")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:categories:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductCategories productCategories)
    {
        //todo
        //根据商户id的不同导出分类
        List<ProductCategories> list = productCategoriesService.selectProductCategoriesList(productCategories);
        ExcelUtil<ProductCategories> util = new ExcelUtil<ProductCategories>(ProductCategories.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @ApiOperation(value = "获取商品分类详细信息")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:categories:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(productCategoriesService.selectProductCategoriesById(id));
    }

    /**
     * 新增商品分类
     */
    @ApiOperation(value = "新增商品分类")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:categories:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCategories productCategories)
    {
        if (productCategoriesService.insertProductCategories(productCategories)){
            return AjaxResult.success("新增成功");
        }else {
            return AjaxResult.success("新增失败，且图片上传取消");
        }
    }

    /**
     * 修改商品分类
     */
    @ApiOperation(value = "修改商品分类")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:categories:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCategoriesRequest request)
    {
        if (productCategoriesService.updateProductCategories(request)){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.success("修改失败，且图片上传取消");
        }

    }

    /**
     * 删除商品分类
     */
    @ApiOperation(value = "删除商品分类")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:categories:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(productCategoriesService.deleteProductCategoriesByIds(ids));
    }
}
